package com.yfbao.horizon.schedule.tk.meter;


import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.binder.MeterBinder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.system.SystemProperties;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class DistributeLockMetrics implements MeterBinder {

    private static Counter successGet; //获取锁成功数
    private static Counter failGet;  //获取锁失败数
    private static Counter successRelease; //释放锁成功数
    private static Counter failRelease;  //释放锁失败数
    private static  Timer getTimer; //xxxx
    private static  Timer holdTimer; //xxxx
    private static  Timer getFailedTimer; //xxxx

    @Override
    public void bindTo(MeterRegistry registry) {
        failGet = registry.counter("distributeLock.get.fail.count", Tags.of("state", "fail"));
        successGet = registry.counter("distributeLock.get.success.count", Tags.of("state", "success"));
        failRelease = registry.counter("distributeLock.release.fail.count", Tags.of("state", "fail"));
        successRelease = registry.counter("distributeLock.release.success.count", Tags.of("state", "success"));
        getTimer = registry.timer("distributeLock.get.time", Tags.of("state", "get"));
        holdTimer = registry.timer("distributeLock.hold.time", Tags.of("state", "hold"));
        getFailedTimer = registry.timer("distributeLock.get.time", Tags.of("state", "failed"));
    }

    public static void successGet(){
        try {

                successGet.increment();

        }catch (Exception e){
        }
    }

    public static void failGet(){
        try {

                failGet.increment();

        }catch (Exception e){
        }
    }

    public static void successRelease(){
        try {

                successRelease.increment();

        }catch (Exception e){
        }
    }

    public static void failRelease(){
        try {

                failRelease.increment();

        }catch (Exception e){
        }
    }

    public static void getCostTime(Long costTime){
        try {
            getTimer.record(costTime, TimeUnit.MILLISECONDS);

        }catch (Exception e){
        }
    }

    public static void holdCostTime(Long holdTime){
        try {

        }catch (Exception e){
        }
    }

}
